package com.waypolice.manager.controller;

import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.common.utils.StringUtil;
import com.waypolice.manager.service.CmsSinItemService;
import com.waypolice.manager.service.VolAndItemService;
import com.waypolice.mapper.CmsSinItemMapper;
import com.waypolice.pojo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lzh
 * @date 2019/03/18
 */
@Controller
public class KeepingArchives {

    @Autowired
    private VolAndItemService volAndItemService;

    @Autowired
    private CmsSinItemService cmsSinItemService;

    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;

    @ApiOperation(value = "档案保管跳转")
    @RequiresPermissions("cms:storage:read")
    @RequestMapping("/manage/arcManage/warRack")
    public String index() {
        return "/manage/keepArchives/keepingArchives";
    }

    @RequestMapping("/pcwindow/keep/keepVolList")
    @ResponseBody
    public Object keepVolList(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                              @RequestParam(required = false, defaultValue = "25", value = "limit") int limit,
                              @RequestParam(required = false, value = "sort") String sort,
                              @RequestParam(required = false, value = "order") String order,
                              String volTitle, String volNO, String volStoragelocation,String sin_itemType) {

        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        if (StringUtils.isNotBlank(volTitle)) {
            criteria.andVolTitleLike("%" + volTitle + "%");
        }
        if (StringUtils.isNotBlank(volNO)) {
            //案卷号为VolNo
            criteria.andVolNoEqualTo(volNO);
        }
        if (StringUtils.isNotBlank(volStoragelocation)) {
            criteria.andVolStoragelocationLike("%" + volStoragelocation + "%");
        }
        if (StringUtils.isNotBlank(sin_itemType)){
            criteria.andVolClassEqualTo(sin_itemType);
        }
            //已归档
            criteria.andVolArchiveStatusEqualTo(2);
        //档案分类

        //按生成时间，升序排列
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            sort= StringUtil.frontStrongToOracleString(sort);
            cmsJudVolumeExample.setOrderByClause(sort + " " + order);
        }else {
            cmsJudVolumeExample.setOrderByClause("\"VOL_CREATE_DATE\" DESC");
        }
        List<CmsJudVolume> cmsJudVolumes  = volAndItemService.selectAllByQueryParms(cmsJudVolumeExample);


        //最终的list集合,分页操作
        PageUtils<CmsJudVolume> pageUtils = new PageUtils<CmsJudVolume>();
        List<CmsJudVolume> cmsJudVolumesPage = pageUtils.pageList(cmsJudVolumes, offset, limit);

        Map<String, Object> result = new HashMap<>();
        result.put("rows", cmsJudVolumesPage);
        result.put("total", cmsJudVolumes.size());
        return result;

    }

    @RequestMapping("/manage/keep/update")
    public String updateinfo() {
        return "/manage/keepArchives/update";
    }

    @RequestMapping("/manage/keep/saveBoxAndLocation")
    @ResponseBody
    public Object saveBoxAndLocation(String ids, String boxCode, String locationCode,String tradOrSinRecord2) {
        boolean bool = StringUtils.isNotBlank(ids);
        int i = 0;
        if (bool) {
            String[] split = ids.split("-");
            for (String id : split) {
                if("0".equals(tradOrSinRecord2)){
                    CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(Long.parseLong(id));
                    cmsJudVolume.setVolBoxNumber(boxCode);
                    cmsJudVolume.setVolStoragelocation(locationCode);
                    cmsJudVolume.setVolEditDate(new Date());
                    i += volAndItemService.updateByPrimaryKeySelectiveNoCheck(cmsJudVolume);
                }else {
                    CmsSinItem cmsSinItem = cmsSinItemMapper.selectByPrimaryKey(Long.valueOf(id));
                    cmsSinItem.setItemBoxNumber(boxCode);
                    cmsSinItem.setItemStoragelocation(locationCode);
                    cmsSinItem.setItemEditDate(new Date());
                    i += cmsSinItemMapper.updateByPrimaryKeySelective(cmsSinItem);
                }

            }
        }
        return i;
    }

    @RequestMapping("/manage/keep/refuse")
    @ResponseBody
    public String reduseArchive(String volIds){
        if(StringUtils.isNotBlank(volIds)){
            String[] idArray = volIds.split("-");
            int flags=0;
            for (String volId : idArray) {
                CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(Long.valueOf(volId));
                cmsJudVolume.setVolArchiveStatus(4);
                cmsJudVolume.setVolEditDate(new Date());
                Integer i = volAndItemService.updateByPrimaryKeySelectiveNoCheck(cmsJudVolume);
                if(i.intValue()==1){
                    flags+=1;
                }
            }
            if(flags == idArray.length){
                return "success";
            }else {
                return "error";
            }
        }
        return "error";
    }

    @RequestMapping("/manage/keep/sinRefuse")
    @ResponseBody
    public Object sinRefuse(String itemIds){
        if(StringUtils.isNotBlank(itemIds)){
            String[] idArray = itemIds.split("-");
            int flagsItem=0;
            for (String itemId : idArray) {
                CmsSinItem cmsSinItem = cmsSinItemMapper.selectByPrimaryKey(Long.valueOf(itemId));
                cmsSinItem.setItemArcStatus(2L);
                cmsSinItem.setItemEditDate(DateTimeUtil.getNow());
                int i1 = cmsSinItemMapper.updateByPrimaryKeySelective(cmsSinItem);

                if(i1==1){
                    flagsItem+=1;
                }
            }
            if(flagsItem == idArray.length){
                return "success";
            }else {
                return "error";
            }
        }
        return "error";
    }

    @ApiOperation(value = "单件归档数据查询")
    @RequestMapping(value = "/manage/keep/itemSinListManage", method = RequestMethod.GET)
    @ResponseBody
    public Object itemSinListManage(HttpServletRequest request,
                              @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                              @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                              String sin_itemTitle, String sin_itemType) {

        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria criteria = cmsSinItemExample.createCriteria();
        if(StringUtils.isNotBlank(sin_itemTitle)){
            criteria.andItemTitleLike("%"+sin_itemTitle+"%");
        }
        if(StringUtils.isNotBlank(sin_itemType)){
            criteria.andItemExp1EqualTo(sin_itemType);
        }
        //已归档
        criteria.andItemArcStatusEqualTo(1L);
        cmsSinItemExample.setOrderByClause("\"ITEM_CREATE_DATE\" DESC");
        List<CmsSinItem> cmsSinItem = cmsSinItemService.selectAllByQueryParms(cmsSinItemExample);

        //最终的list集合,分页操作
        PageUtils<CmsSinItem> pageUtils = new PageUtils<CmsSinItem>();
        List<CmsSinItem> cmsSinItemPage = pageUtils.pageList(cmsSinItem, offset, limit);

        Map<String, Object> result = new HashMap<>();
        result.put("rows",cmsSinItemPage);
        result.put("total",cmsSinItem.size());
        return result;
    }
}